package com.cxsj.project.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxsj.framework.web.domain.JsonResult;
import com.cxsj.project.system.entity.User;
import com.cxsj.project.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dempsey
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/api/customer")
public class UserController {

    @Autowired
    private IUserService userService;

    //接口1：get请求('/api/customer/user'）
    @GetMapping("/list")
    public JsonResult list() {
        List<User> list = userService.list();
        return JsonResult.result(list);
    }

    //接口2：get请求（'/api/customer/login',params）
    //传入参数	name: values.userName,
    //		password: values.password,
    //结果：是否跟数据库中的用户名密码对应上?{_id:key,code:1}:{}
    @GetMapping("/login")
    public JsonResult login(String username, String password) {
        User user = userService.getOne(new QueryWrapper<User>().eq("name", username).eq("password", password));
        if (user != null) {
            JsonResult jsonResult = JsonResult.success();
            jsonResult.put("_id",user.getId());
            return jsonResult;
        }
        return JsonResult.result("登录失败");
    }
}

